// EnumEjerforhold.cpp : Implementation of CEnumEjerforhold
#include "stdafx.h"
#include "EBASdll.h"
#include "EnumEjerforhold.h"
#include "Ejerforhold.h"

/////////////////////////////////////////////////////////////////////////////
// CEnumEjerforhold

STDMETHODIMP CEnumEjerforhold::InterfaceSupportsErrorInfo(REFIID riid)
{
	static const IID* arr[] = 
	{
		&IID_IEnumEjerforhold
	};
	for (int i=0; i < sizeof(arr) / sizeof(arr[0]); i++)
	{
		if (InlineIsEqualGUID(*arr[i],riid))
			return S_OK;
	}
	return S_FALSE;
}

CEnumEjerforhold::CEnumEjerforhold()
{
	m_pUnkMarshaler = NULL;
}

CEnumEjerforhold::~CEnumEjerforhold()
{
	if (m_records)
		m_records->Close();
}


STDMETHODIMP CEnumEjerforhold::Next(/*[out]*/ IUnknown ** ppUnk, /*[out]*/ ULONG * pcFetched)
{
	_CommandPtr cmd;
	
	bstr_t guid, leder;
	
	const char sql[255] = "EBAS_SP_HENT_EJERFORHOLD";

	try {
		if (!ppUnk)
			return E_POINTER;

		ULONG cFetched = 0;

		if (m_first) {
			m_first = FALSE;

			cmd.CreateInstance(__uuidof(Command));
			cmd->ActiveConnection = CDatabase::GetConnection();
			cmd->CommandText = sql;
			cmd->CommandType = adCmdStoredProc;
			cmd->Parameters->Append( cmd->CreateParameter("session", adVarChar, adParamInput, 255, gpSessionGuid) );

			m_records = cmd->Execute(NULL, NULL, adCmdStoredProc);	
			if (m_records->ADO_EOF)
				return E_ACCESSDENIED;
		}			

		if (m_records->ADO_EOF)
			return S_OK;

		guid = m_records->Fields->GetItem("GUID")->Value;
		leder = m_records->Fields->GetItem("LEDER")->Value;

		m_length = (long)m_records->Fields->GetItem("ANTAL")->Value;

		CComObject<CEjerforhold> * pInstance;
		CComObject<CEjerforhold>::CreateInstance(&pInstance);
		pInstance->put_Leder(leder);
		pInstance->put_GUID(guid);

		CComObject<CAdresse> *  pAdresse;
		CComObject<CAdresse>::CreateInstance(&pAdresse);
		pAdresse->put_GUID( (bstr_t)m_records->Fields->GetItem("ADRESSE_GUID")->Value );
		pAdresse->put_Attention( (bstr_t)m_records->Fields->GetItem("ATTENTION")->Value );
		pAdresse->put_Gade( (bstr_t)m_records->Fields->GetItem("GADE")->Value );
		pAdresse->put_PostNummer( (bstr_t)m_records->Fields->GetItem("POST_NR")->Value );
		pAdresse->put_Email( (bstr_t)m_records->Fields->GetItem("E_MAIL")->Value );
		pAdresse->put_AdresseType( (bstr_t)m_records->Fields->GetItem("TYPE")->Value );
		pAdresse->put_Telefon( (bstr_t)m_records->Fields->GetItem("TELEFON")->Value );
		pAdresse->put_Telefax( (bstr_t)m_records->Fields->GetItem("TELEFAX")->Value );

		IAdresse * ppAdresse = NULL;
		pAdresse->QueryInterface(__uuidof(IAdresse), (void**)&ppAdresse);
		pInstance->put_Adresse( ppAdresse );
		ppAdresse->Release();

		pInstance->QueryInterface(ppUnk);
		m_pos += 1;
		cFetched = m_length;

		if (pcFetched)
			*pcFetched = cFetched;		

		m_records->MoveNext();

	} catch (_com_error &e) {
		char err[512];
		strncpy(err, e.ErrorMessage(), 512);
		strncpy(err, " ", 1);
		strncpy(err, e.Description(), _tcslen(e.Description()));
		CErrorHandling::SetErrorMessage(e.ErrorMessage());
		return e.Error();
	}

	return TRUE;
}

STDMETHODIMP CEnumEjerforhold::Skip(ULONG cItems)
{
	try {
		if (m_records) {
			m_pos += cItems;
			m_records->Move(m_pos);
		}
	} catch (_com_error &e) {

		return e.Error();
	}

	return S_OK;
}

STDMETHODIMP CEnumEjerforhold::Reset()
{
	_CommandPtr cmd;

	const char sql[255] = "EBAS_SP_HENT_EJERFORHOLD";

	try {
		cmd.CreateInstance(__uuidof(Command));
		cmd->ActiveConnection = CDatabase::GetConnection();
		cmd->CommandText = sql;
		cmd->CommandType = adCmdStoredProc;
		cmd->Parameters->Append( cmd->CreateParameter("session", adVarChar, adParamInput, 255, gpSessionGuid) );

		m_records = cmd->Execute(NULL, NULL, adCmdStoredProc);
		if (m_records->ADO_EOF)
			return E_ACCESSDENIED;	
	} catch (_com_error &e) {
		return e.Error();
	}

	return S_OK;
}

STDMETHODIMP CEnumEjerforhold::Clone(IEnumeration ** ppEnumeration)
{
	return E_NOTIMPL;
}

STDMETHODIMP CEnumEjerforhold::findEjerforhold(BSTR navn, IEjerforhold ** pEjerforhold)
{
	_CommandPtr cmd;
	_RecordsetPtr recordset;
	
	bstr_t guid, leder;
	
	const char sql[255] = "EBAS_SP_HENT_EJERFORHOLD";

	try {
		if (!pEjerforhold)
			return E_POINTER;

		cmd.CreateInstance(__uuidof(Command));
		cmd->ActiveConnection = CDatabase::GetConnection();
		cmd->CommandText = sql;
		cmd->CommandType = adCmdStoredProc;
		cmd->Parameters->Append( cmd->CreateParameter("session", adVarChar, adParamInput, 255, gpSessionGuid) );
		cmd->Parameters->Append( cmd->CreateParameter("attention", adVarChar, adParamInput, 255, navn) );

		recordset = cmd->Execute(NULL, NULL, adCmdStoredProc);	
		if (recordset->ADO_EOF)
			return E_ACCESSDENIED;		

		guid = recordset->Fields->GetItem("GUID")->Value;
		leder = recordset->Fields->GetItem("LEDER")->Value;

		CComObject<CEjerforhold> * pInstance;
		CComObject<CEjerforhold>::CreateInstance(&pInstance);
		pInstance->put_Leder(leder);
		pInstance->put_GUID(guid);

		CComObject<CAdresse> *  pAdresse;
		CComObject<CAdresse>::CreateInstance(&pAdresse);
		pAdresse->put_GUID( (bstr_t)recordset->Fields->GetItem("ADRESSE_GUID")->Value );
		pAdresse->put_Attention( (bstr_t)recordset->Fields->GetItem("ATTENTION")->Value );
		pAdresse->put_Gade( (bstr_t)recordset->Fields->GetItem("GADE")->Value );
		pAdresse->put_PostNummer( (bstr_t)recordset->Fields->GetItem("POST_NR")->Value );
		pAdresse->put_Email( (bstr_t)recordset->Fields->GetItem("E_MAIL")->Value );
		pAdresse->put_AdresseType( (bstr_t)recordset->Fields->GetItem("TYPE")->Value );
		pAdresse->put_Telefon( (bstr_t)recordset->Fields->GetItem("TELEFON")->Value );
		pAdresse->put_Telefax( (bstr_t)recordset->Fields->GetItem("TELEFAX")->Value );
		
		if (recordset)
			recordset->Close();

		IAdresse * ppAdresse = NULL;
		pAdresse->QueryInterface(__uuidof(IAdresse), (void**)&ppAdresse);
		pInstance->put_Adresse( ppAdresse );
		ppAdresse->Release();

		pInstance->QueryInterface(pEjerforhold);

	} catch (_com_error &e) {
		char err[512];
		strncpy(err, e.ErrorMessage(), 512);
		strncpy(err, " ", 1);
		strncpy(err, e.Description(), _tcslen(e.Description()));
		CErrorHandling::SetErrorMessage(e.ErrorMessage());
		return e.Error();
	}

	return TRUE;
}

STDMETHODIMP CEnumEjerforhold::findEjerforholdGuid(BSTR guid, IEjerforhold **ppEjerforhold)
{
	_CommandPtr cmd;
	_RecordsetPtr recordset;
	
	bstr_t eGuid, leder;
	
	const char sql[255] = "EBAS_SP_HENT_EJERFORHOLD";

	try {
		if (!ppEjerforhold)
			return E_POINTER;

		cmd.CreateInstance(__uuidof(Command));
		cmd->ActiveConnection = CDatabase::GetConnection();
		cmd->CommandText = sql;
		cmd->CommandType = adCmdStoredProc;
		cmd->Parameters->Append( cmd->CreateParameter("session", adVarChar, adParamInput, 255, gpSessionGuid) );
		cmd->Parameters->Append( cmd->CreateParameter("attention", adVarChar, adParamInput, 255, "%") );
		cmd->Parameters->Append( cmd->CreateParameter("guid", adVarChar, adParamInput, 255, guid) );

		recordset = cmd->Execute(NULL, NULL, adCmdStoredProc);	
		if (recordset->ADO_EOF)
			return E_ACCESSDENIED;		

		eGuid = recordset->Fields->GetItem("GUID")->Value;
		leder = recordset->Fields->GetItem("LEDER")->Value;

		CComObject<CEjerforhold> * pInstance;
		CComObject<CEjerforhold>::CreateInstance(&pInstance);
		pInstance->put_Leder(leder);
		pInstance->put_GUID(eGuid);

		CComObject<CAdresse> *  pAdresse;
		CComObject<CAdresse>::CreateInstance(&pAdresse);
		pAdresse->put_GUID( (bstr_t)recordset->Fields->GetItem("ADRESSE_GUID")->Value );
		pAdresse->put_Attention( (bstr_t)recordset->Fields->GetItem("ATTENTION")->Value );
		pAdresse->put_Gade( (bstr_t)recordset->Fields->GetItem("GADE")->Value );
		pAdresse->put_PostNummer( (bstr_t)recordset->Fields->GetItem("POST_NR")->Value );
		pAdresse->put_Email( (bstr_t)recordset->Fields->GetItem("E_MAIL")->Value );
		pAdresse->put_AdresseType( (bstr_t)recordset->Fields->GetItem("TYPE")->Value );
		pAdresse->put_Telefon( (bstr_t)recordset->Fields->GetItem("TELEFON")->Value );
		pAdresse->put_Telefax( (bstr_t)recordset->Fields->GetItem("TELEFAX")->Value );
		
		if (recordset)
			recordset->Close();

		IAdresse * ppAdresse = NULL;
		pAdresse->QueryInterface(__uuidof(IAdresse), (void**)&ppAdresse);
		pInstance->put_Adresse( ppAdresse );
		ppAdresse->Release();

		pInstance->QueryInterface(ppEjerforhold);

	} catch (_com_error &e) {
		char err[512];
		strncpy(err, e.ErrorMessage(), 512);
		strncpy(err, " ", 1);
		strncpy(err, e.Description(), _tcslen(e.Description()));
		CErrorHandling::SetErrorMessage(e.ErrorMessage());
		return e.Error();
	}

	return TRUE;
}

STDMETHODIMP CEnumEjerforhold::findEjerforholdLeder(BSTR leder, IEjerforhold **ppEjerforhold)
{
	_CommandPtr cmd;
	_RecordsetPtr recordset;
	
	bstr_t eGuid, bstrLeder;
	
	const char sql[255] = "EBAS_SP_HENT_EJERFORHOLD_LEDER";

	try {
		if (!ppEjerforhold)
			return E_POINTER;

		bstrLeder = leder;

		cmd.CreateInstance(__uuidof(Command));
		cmd->ActiveConnection = CDatabase::GetConnection();
		cmd->CommandText = sql;
		cmd->CommandType = adCmdStoredProc;
		cmd->Parameters->Append( cmd->CreateParameter("session", adVarChar, adParamInput, 255, gpSessionGuid) );
		cmd->Parameters->Append( cmd->CreateParameter("leder", adVarChar, adParamInput, 255, bstrLeder) );

		recordset = cmd->Execute(NULL, NULL, adCmdStoredProc);	
		if (recordset->ADO_EOF)
			return E_ACCESSDENIED;		

		eGuid = (bstr_t)recordset->Fields->GetItem("GUID")->Value;
		bstrLeder = (bstr_t)recordset->Fields->GetItem("LEDER")->Value;

		CComObject<CEjerforhold> * pInstance;
		CComObject<CEjerforhold>::CreateInstance(&pInstance);
		pInstance->put_Leder(bstrLeder);
		pInstance->put_GUID(eGuid);

		CComObject<CAdresse> *  pAdresse;
		CComObject<CAdresse>::CreateInstance(&pAdresse);
		pAdresse->put_GUID( (bstr_t)recordset->Fields->GetItem("ADRESSE_GUID")->Value );
		pAdresse->put_Attention( (bstr_t)recordset->Fields->GetItem("ATTENTION")->Value );
		pAdresse->put_Gade( (bstr_t)recordset->Fields->GetItem("GADE")->Value );
		pAdresse->put_PostNummer( (bstr_t)recordset->Fields->GetItem("POST_NR")->Value );
		pAdresse->put_Email( (bstr_t)recordset->Fields->GetItem("E_MAIL")->Value );
		pAdresse->put_AdresseType( (bstr_t)recordset->Fields->GetItem("TYPE")->Value );
		pAdresse->put_Telefon( (bstr_t)recordset->Fields->GetItem("TELEFON")->Value );
		pAdresse->put_Telefax( (bstr_t)recordset->Fields->GetItem("TELEFAX")->Value );
		
		if (recordset)
			recordset->Close();

		//IAdresse * ppAdresse = NULL;
		//pAdresse->QueryInterface(__uuidof(IAdresse), (void**)&ppAdresse);
		//ppAdresse->Release();

		pInstance->put_Adresse( pAdresse );
		pInstance->QueryInterface(ppEjerforhold);
	} catch (_com_error &e) {
		char err[512];
		strncpy(err, e.ErrorMessage(), 512);
		strncpy(err, " ", 1);
		strncpy(err, e.Description(), _tcslen(e.Description()));
		CErrorHandling::SetErrorMessage(e.ErrorMessage());
		return e.Error();
	}

	return TRUE;
}
